<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>open</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_007_919">&nbsp;</a>NAME</h4><blockquote>
open - open a file
</blockquote><h4><a name = "tag_000_007_920">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="systypes.h.html">sys/types.h</a>&gt;
#include &lt;<a href="sysstat.h.html">sys/stat.h</a>&gt;
#include &lt;<a href="fcntl.h.html">fcntl.h</a>&gt;

int open(const char *<i>path</i>, int <i>oflag</i>, ... );
</code>
</pre>
</blockquote><h4><a name = "tag_000_007_921">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>open()</i>
function establishes the connection between a file and a
file descriptor.
It creates an open file description that refers to a file
and a file descriptor that refers to that open file description.
The file descriptor is used by other I/O functions to refer to that file.  The
<i>path</i>
argument points to a pathname naming the file.
<p>
The
<i>open()</i>
function will return a file descriptor for the named file
that is the lowest file descriptor not currently open for
that process.
The open file description is new, and therefore the file
descriptor does not share it with any other process in the
system.  The FD_CLOEXEC file descriptor flag associated with the new file
descriptor will be cleared.
<p>
The file offset used to mark the current
position within the file is set to the beginning of the file.
<p>
The file status flags and file access modes of the open file
description will be set according to the value of
<i>oflag</i>.
<p>
Values for
<i>oflag</i>
are constructed by a bitwise-inclusive-OR of
flags from the following list, defined in
<i><a href="fcntl.h.html">&lt;fcntl.h&gt;</a></i>.
Applications must specify exactly one of the first three
values (file access modes) below in the value of
<i>oflag</i>:
<dl compact>

<dt>O_RDONLY<dd>
Open for reading only.

<dt>O_WRONLY<dd>
Open for writing only.

<dt>O_RDWR<dd>
Open for reading and writing. The result is undefined if this flag is
applied to a FIFO.

</dl>
<p>
Any combination of the following may be used:
<dl compact>

<dt>O_APPEND<dd>
If set, the file offset will be set to the end of the file
prior to each write.

<dt>O_CREAT<dd>
If the file exists, this flag has no effect
except as noted under O_EXCL below.
Otherwise, the file is created; the user ID
of the file is set to the effective user ID
of the process; the group ID of the file is set to
&nbsp;the group ID of the file's parent directory or to the effective group ID
of the process;
&nbsp;and the access permission bits (see
<i><a href="sysstat.h.html">&lt;sys/stat.h&gt;</a></i>)
of the file mode are set to the value of
the third argument taken as type
<b>mode_t</b>
modified as follows:
a bitwise-AND is performed on the file-mode bits and the corresponding
bits in the complement of the process' file mode creation mask.
Thus, all bits in the file mode whose corresponding bit in the
file mode creation mask is set are cleared.
When bits other than the file permission bits are set, the effect is
unspecified. The third argument does not affect whether
the file is open for reading, writing or for both.

<dt>O_DSYNC<dd>
Write I/O operations on the file descriptor complete as defined by
synchronised I/O data integrity completion

<dt>O_EXCL<dd>
If O_CREAT and O_EXCL are set,
<i>open()</i>
will fail if the file exists.
The check for the existence of the file and the creation of the
file if it does not exist will be atomic with respect to other
processes executing
<i>open()</i>
naming the same filename in the same directory
with O_EXCL and O_CREAT set.
If O_CREAT is not set, the effect is undefined.

<dt>O_NOCTTY<dd>
If set and
<i>path</i>
identifies a terminal device,
<i>open()</i>
will not cause the terminal device to become the controlling
terminal for the process.

<dt>O_NONBLOCK<dd>
When opening a FIFO with O_RDONLY or O_WRONLY set:

If O_NONBLOCK is set:
<dl compact><dt> <dd>
An
<i>open()</i>
for reading only will return without delay.  An
<i>open()</i>
for writing only will return an error if no process
currently has the file open for reading.
</dl>
<p>
If O_NONBLOCK is clear:
<dl compact><dt> <dd>
An
<i>open()</i>
for reading only will block 
the calling thread
until a thread 
opens the file for writing.  An
<i>open()</i>
for writing only will block 
the calling thread
until a thread
opens the file for reading.
</dl>
<p>
When opening a block special or character special
file that supports non-blocking opens:
<p>
If O_NONBLOCK is set:
<dl compact><dt> <dd>
The
<i>open()</i>
function will return without blocking for the device
to be ready or available.
Subsequent behaviour of the device is device-specific.
</dl>
<p>
If O_NONBLOCK is clear:
<dl compact><dt> <dd>
The
<i>open()</i>
function will block 
the calling thread
until the device is ready or available before returning.
</dl>
<p>
Otherwise, the behaviour of O_NONBLOCK is unspecified.
<p>
<dt>O_RSYNC<dd>
Read I/O operations on the file descriptor complete at the
same level of integrity as specified by the O_DSYNC and
O_SYNC flags.  If both O_DSYNC and O_RSYNC are set in
<i>oflag,</i>
all I/O operations on the file descriptor complete as defined by
synchronised I/O data integrity completion.
If both O_SYNC and O_RSYNC are set in flags, all I/O operations on the
file descriptor complete as defined by synchronised I/O file integrity
completion.
<p>
<dt>O_SYNC<dd>
Write I/O operations on the file descriptor complete as defined by
synchronised I/O file integrity completion.
<p>
<dt>O_TRUNC<dd>
If the file exists and is a regular file, and the file is successfully
opened O_RDWR or O_WRONLY, its length
is truncated to 0 and the mode and owner are unchanged. It will have no
effect on FIFO special files or
terminal device files. Its effect on other file
types is implementation-dependent. The result of using O_TRUNC with
O_RDONLY is undefined.
<p>
</dl>
<p>
If O_CREAT is set and the file did not previously exist, upon successful
completion,
<i>open()</i>
will mark for update the
<i>st_atime,</i>
<i>st_ctime</i>
and
<i>st_mtime</i>
fields of the file and the
<i>st_ctime</i>
and
<i>st_mtime</i>
fields of the parent directory.
<p>
If O_TRUNC is set and the file did previously exist,
upon successful completion,
<i>open()</i>
will mark for update the
<i>st_ctime</i>
and
<i>st_mtime</i>
fields of the file.
<p>
If both the O_SYNC and O_DSYNC
flags are set, the effect is as if only the
O_SYNC flag was set.
<p>
If <i>path</i> refers to a STREAMS file,
<i>oflag</i>
may be constructed from O_NONBLOCK OR-ed with either O_RDONLY, O_WRONLY or
O_RDWR.
Other flag values are not applicable to STREAMS devices and have no effect on
them.  The value O_NONBLOCK affects the operation of STREAMS drivers and
certain functions applied to file descriptors associated with STREAMS files.
For STREAMS drivers, the implementation of O_NONBLOCK is device-specific.
<p>
If
<i>path</i>
names the master side of a pseudo-terminal device, then it is unspecified
whether
<i>open()</i>
locks the slave side so that it cannot be opened.  Portable applications must
call
<i><a href="unlockpt.html">unlockpt()</a></i>
before opening the slave side.
<p>
The largest value that can be represented correctly in an object of
type
<b>off_t</b>
will be established as the offset maximum in the open file description. 
</blockquote><h4><a name = "tag_000_007_922">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion, the function will open the file
and return a non-negative integer representing the lowest numbered
unused file descriptor.
Otherwise, -1 is returned and
<i>errno</i>
is set to indicate the error.
No files will be created or modified if the function returns -1.
</blockquote><h4><a name = "tag_000_007_923">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>open()</i>
function will fail if:
<dl compact>

<dt>[EACCES]<dd>
Search permission is denied on a component of the path prefix,
or the file exists and the permissions specified by
<i>oflag</i>
are denied, or the file does not exist and write permission is
denied for the parent directory of the file to be created, or
O_TRUNC is specified and write permission is denied.

<dt>[EEXIST]<dd>
O_CREAT and O_EXCL are set, and the named file exists.

<dt>[EINTR]<dd>
A signal was caught during
<i>open()</i>.

<dt>[EINVAL]<dd>
The implementation does not support synchronised I/O for this file.

<dt>[EIO]<dd>
The <i>path</i> argument names a STREAMS file and a hangup or error occurred
during the
<i>open()</i>.

<dt>[EISDIR]<dd>
The named file is a directory and
<i>oflag</i>
includes O_WRONLY or O_RDWR.

<dt>[ELOOP]<dd>
Too many symbolic links were encountered in resolving <i>path</i>.

<dt>[EMFILE]<dd>
{OPEN_MAX}
file descriptors are currently open in the calling process.

<dt>[ENAMETOOLONG]<dd>

The length of the
<i>path</i>
argument exceeds {PATH_MAX} or a pathname component is longer than
{NAME_MAX}.

<dt>[ENFILE]<dd>
The maximum allowable number of files is currently open in the
system.

<dt>[ENOENT]<dd>
O_CREAT is not set and the named file does not exist;
or O_CREAT is set and either the path prefix does not exist
or the
<i>path</i>
argument points to an empty string.

<dt>[ENOSR]<dd>
The <i>path</i> argument names a STREAMS-based file
and the system is unable to allocate a STREAM.

<dt>[ENOSPC]<dd>
The directory or file system that would contain the
new file cannot be expanded, the file does not exist, and
O_CREAT is specified.

<dt>[ENOTDIR]<dd>
A component of the path prefix is not a directory.


<dt>[ENXIO]<dd>
O_NONBLOCK is set, the named file is a FIFO,
O_WRONLY is set and no process has
the file open for reading.

<dt>[ENXIO]<dd>
The named file is a character special or block special file,
and the device associated with this special file does not exist.

<dt>[EOVERFLOW]<dd>
The named file is a regular file and the size of the file cannot be
represented correctly in an object of type
<b>off_t</b>.

<dt>[EROFS]<dd>
The named file resides on a read-only file system and
either O_WRONLY, O_RDWR, O_CREAT
(if file does not exist) or O_TRUNC is set in the
<i>oflag</i>
argument.

</dl>
<p>
The
<i>open()</i>
function may fail if:
<dl compact>

<dt>[EAGAIN]<dd>
The
<i>path</i>
argument names the slave side of a pseudo-terminal device that is locked.

<dt>[EINVAL]<dd>
The
value of the
<i>oflag</i>
argument is not valid.

<dt>[ENAMETOOLONG]<dd>

Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds {PATH_MAX}.

<dt>[ENOMEM]<dd>
The <i>path</i> argument names a STREAMS file and the system is unable to
allocate resources.

<dt>[ETXTBSY]<dd>
The file is a pure procedure (shared text) file that is being
executed and
<i>oflag</i>
is O_WRONLY or O_RDWR.

</dl>
</blockquote><h4><a name = "tag_000_007_924">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_007_925">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_007_926">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_007_927">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="chmod.html">chmod()</a></i>,
<i><a href="close.html">close()</a></i>,
<i><a href="creat.html">creat()</a></i>,
<i><a href="dup.html">dup()</a></i>,
<i><a href="fcntl.html">fcntl()</a></i>,
<i><a href="lseek.html">lseek()</a></i>,
<i><a href="read.html">read()</a></i>,
<i><a href="umask.html">umask()</a></i>,
<i><a href="unlockpt.html">unlockpt()</a></i>,
<i><a href="write.html">write()</a></i>,
<i><a href="fcntl.h.html">&lt;fcntl.h&gt;</a></i>,
<i><a href="sysstat.h.html">&lt;sys/stat.h&gt;</a></i>,
<i><a href="systypes.h.html">&lt;sys/types.h&gt;</a></i>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 1 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

